package icraf.carbon.core;


import java.util.Comparator;

public class DataComparator implements Comparator<AbacusData> {
	private boolean absolute = false;
	private boolean ascending = true;
	private CostBenefitUnit costBenefitUnit; 
	
	public CostBenefitUnit getCostBenefitUnit() {
		return costBenefitUnit;
	}

	public void setCostBenefitUnit(CostBenefitUnit costBenefitUnit) {
		this.costBenefitUnit = costBenefitUnit;
	}

	@Override
	public int compare(AbacusData o1, AbacusData o2) {
		double v1;
		double v2;

		if(costBenefitUnit == null)
			return 0;
		if (ascending) {
			v1 = absolute ? Math.abs(o1.getCost(costBenefitUnit)) : o1.getCost(costBenefitUnit);
			v2 = absolute ? Math.abs(o2.getCost(costBenefitUnit)) : o2.getCost(costBenefitUnit);
		} else {
			v1 = absolute ? Math.abs(o2.getCost(costBenefitUnit)) : o2.getCost(costBenefitUnit);
			v2 = absolute ? Math.abs(o1.getCost(costBenefitUnit)) : o1.getCost(costBenefitUnit);
		}

		if (Math.signum(o1.getEmission()) < Math.signum(o2.getEmission()))
			return -1;
		if (Math.signum(o1.getEmission()) > Math.signum(o2.getEmission()))
			return 1;

		if (v1 < v2)
			return -1;
		if (v1 > v2)
			return 1;
		return 0;
	}

	public void setAbsolute(boolean absolute) {
		this.absolute = absolute;
	}

	public void setAscending(boolean ascending) {
		this.ascending = ascending;
	}
}